import { getUserDetail } from "@/services";
import Taro from "@tarojs/taro";

export default {
  namespace: "certified",
  state: {
    stepObj: {
      step0: false,
      step1: false,
      step2: false
    },
    showDialog: false,
    statusStep: 0, //认证进度

    userInfo: Taro.getStorageSync("user-info")
      ? JSON.parse(Taro.getStorageSync("user-info"))
      : {}
  },
  effects: {
    // 查询用户详情
    *asyncGetUserDetail({ payload, callback }, { put }) {
      const ret = yield getUserDetail();
      if (ret.code == 200) {
        Taro.setStorageSync("user-info", JSON.stringify(ret.data));
        let userInfo = ret.data ?? {};

        yield put({
          type: "save",
          payload: {
            userInfo: userInfo
          }
        });
        yield put({
          type: "setStepStatus",
          payload: userInfo.userRisk
        });
        callback && callback(ret);
        // resolve(ret);
      }
    }
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    },
    setStepStatus(state, { payload }) {
      let userRisk = payload;
      let step0 =
        userRisk.authCertiStatus == 3 &&
        (userRisk.investorClass === 1 || userRisk.investorClass === 2);
      // 判断合格投资者审核附件存在 即合格投资者承诺已确认
      let step1 = userRisk.investConfirmAttachment ? true : false;
      // 接受等级大于0
      let step2 = userRisk.riskAccept > 0;

      return {
        ...state,
        stepObj: {
          step0,
          step1,
          step2
        },
        showDialog: !step0 || !step1 || !step2
      };
    }
  }
};
